package com.bean;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.greenentity.Task;
import com.substation.R;
import com.substation.ui.activity.DefectActivity;
import com.substation.ui.activity.LoggerActivity;
import com.substation.ui.activity.PatrolActivity;
import com.substation.ui.activity.ReadyActivity;
import com.substation.ui.activity.SafetyCardActivity;
import com.substation.ui.activity.SubstationRecordActivity;
import com.substation.ui.activity.TaskActivity;
import com.substation.utils.SPUtils;
import com.substation.utils.T;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/5/15.
 */

public class HomeItem {

    private int imgRes;
    private String title;
    private View.OnClickListener listener;

    public HomeItem(int imgRes, String title, View.OnClickListener listener) {
        this.imgRes = imgRes;
        this.listener = listener;
        this.title = title;
    }

    public int getImgRes() {
        return imgRes;
    }

    public void setImgRes(int imgRes) {
        this.imgRes = imgRes;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public View.OnClickListener getListener() {
        return listener;
    }

    public void setListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    public static List<HomeItem> getHomeItem(final Context context) {
        List<HomeItem> list = new ArrayList<>();
        list.add(new HomeItem(R.mipmap.icon_1, "变电站档案", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, SubstationRecordActivity.class));
            }
        }));
        list.add(new HomeItem(R.mipmap.icon_2, "任务", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, TaskActivity.class));
            }
        }));
        list.add(new HomeItem(R.mipmap.icon_3, "巡视", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String taskContent = (String) SPUtils.get(context, "task", "");
                if (TextUtils.isEmpty(taskContent)) {
                    T.showLong(context, "您还未选择巡视任务");
                    context.startActivity(new Intent(context, TaskActivity.class));
                    return;
                }
                Task task = JSON.parseObject(taskContent, Task.class);
                if (task.getSafety() == 0) {
                    T.showLong(context, "请先阅读安全须知和选择安全工器具");
                    context.startActivity(new Intent(context, ReadyActivity.class));
                } else {
                    context.startActivity(new Intent(context, PatrolActivity.class));
                }
            }
        }));
        list.add(new HomeItem(R.mipmap.icon_4, "缺陷管理", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, DefectActivity.class));
            }
        }));
        list.add(new HomeItem(R.mipmap.icon_5, "工作日志", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, LoggerActivity.class));
            }
        }));
        list.add(new HomeItem(R.mipmap.icon_6, "监察", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String taskContent = (String) SPUtils.get(context, "task", "");
                if (TextUtils.isEmpty(taskContent)) {
                    T.showLong(context, "您还未选择巡视任务");
                    context.startActivity(new Intent(context, TaskActivity.class));
                    return;
                } else {
                    context.startActivity(new Intent(context, SafetyCardActivity.class));
                }
            }
        }));
        return list;
    }
}
